import { OptionTypes, SUCCESS_CODE } from '@/utils/types.d';
import type { ResponseData } from '@/utils/types.d';
import request from '@/utils/request';
import type {
  CopyScheduleType,
  CreateTollCollector,
  DayScheduleItem,
  QueryMonthSchedule,
  TollCollectorTableItem,
  TollerOperRecordTableItem,
  TollerSignInRecordTableItem,
  UpdateTollCollector,
} from './types.d';

export async function queryTollCollectors(
  data: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: TollCollectorTableItem[];
}> {
  const res: ResponseData<{ total: number; data: TollCollectorTableItem[] }> = await request(
    '/api/toll-collector/v1.0.0/tolls',
    {
      params: {
        data,
        sort,
      },
    },
  );
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function createToller(params: CreateTollCollector) {
  const res = await request('/api/toll-collector/v1.0.0/toll', {
    method: 'POST',
    data: params,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '添加成功',
    };
  return { data: res?.data, success: false, message: res?.message || '添加失败' };
}
export async function updateTollCollector(id: string, newInfo: UpdateTollCollector) {
  if (!id || !newInfo || Object.keys(newInfo).length === 0)
    return { success: false, message: '更新失败，请填入需要更新的信息' };
  const res: ResponseData<null> = await request(`/api/toll-collector/v1.0.0/toll/${id}`, {
    method: 'PUT',
    data: newInfo,
  });
  if (res?.code === SUCCESS_CODE) {
    return { success: true, data: res.data, message: '更新成功' };
  }
  return { success: false, data: res?.data, message: res?.message || '更新失败' };
}

export async function removeTollCollector(id: string) {
  const res: ResponseData<null> = await request(`/api/toll-collector/v1.0.0/toll/${id}`, {
    method: 'DELETE',
  });
  if (res?.code === SUCCESS_CODE) return { success: true, message: '删除成功' };
  return { success: false, message: res?.message || '删除失败' };
}

export async function queryTollerSignRecords(
  data: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: TollerSignInRecordTableItem[];
}> {
  const res: ResponseData<{ total: number; data: TollerSignInRecordTableItem[] }> = await request(
    '/api/toll-collector/v1.0.0/toll/signRecord',
    {
      params: {
        data,
        sort,
      },
    },
  );
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function queryTollerOperRecords(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: TollerOperRecordTableItem[];
}> {
  const res = await request('/api/toll-collector/v1.0.0/pos-end-records', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function getStaffMonthSchedule(
  params: QueryMonthSchedule,
): Promise<DayScheduleItem[]> {
  const res = await request('/api/cashier-manager/v1.0.0/schedule-management/schedule', {
    method: 'GET',
    params: params,
  });
  if (res?.code === SUCCESS_CODE) return res.data;
  return [];
}

export async function getCashierNames(name?: string, ownerId?: string, valueNe?: string) {
  const res: ResponseData<OptionTypes[]> = await request(
    '/api/cashier-manager/v1.0.0/schedule-management/cashiernames',
    {
      params: { name, ownerId },
    },
  );
  if (res?.code === SUCCESS_CODE) {
    return valueNe ? res.data?.filter((item) => item.value != valueNe) : res.data;
  }
  return [];
}

export async function copyCashierSchedule(params: CopyScheduleType) {
  const res = await request('/api/cashier-manager/v1.0.0/schedule-management/copy/schedule', {
    method: 'POST',
    data: params,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: res?.message || '复制成功',
    };
  return { data: res?.data, success: false, message: res?.message || '复制失败' };
}

export async function queryParkinglotNamesForSchedule(
  parkingLotName?: string,
  ownerId?: string,
  currArr: OptionTypes[] = [],
) {
  const res: ResponseData<OptionTypes[]> = await request('/api/parkinglot/v1.0.0/parkingLotNames', {
    params: { parkingLotName, ownerId },
  });
  if (res?.code === SUCCESS_CODE) {
    const finalArr = res?.data;
    const set = new Set();
    for (const item of finalArr) {
      set.add(`${item.label}=${item.value}`);
    }
    for (const item of currArr) {
      if (!set.has(`${item.label}=${item.value}`)) {
        finalArr.push(item);
      }
    }
    return finalArr;
  }

  return currArr;
}
